package com.cskaoyan.bean;

import com.cskaoyan.model.MarketOrder;
import com.cskaoyan.model.MarketOrderGoods;
import com.cskaoyan.model.MarketUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class OrderDetailData {
    private List<OrderGoodsDTO> orderGoods;
    private UserDTO user;
    private OrderDTO order;

    @NoArgsConstructor
    @Data
    public static class UserDTO {
        private String nickname;
        private String avatar;
    }

    @NoArgsConstructor
    @Data
    public static class OrderDTO {
        private Integer id;
        private Integer userId;
        private String orderSn;
        private Short orderStatus;
        private Short aftersaleStatus;
        private String consignee;
        private String mobile;
        private String address;
        private String message;
        private BigDecimal goodsPrice;
        private BigDecimal freightPrice;
        private BigDecimal couponPrice;
        private BigDecimal integralPrice;
        private BigDecimal grouponPrice;
        private BigDecimal orderPrice;
        private BigDecimal actualPrice;
        private Integer comments;
        private Date addTime;
        private Date updateTime;
        private Boolean deleted;
    }

    @NoArgsConstructor
    @Data
    public static class OrderGoodsDTO {
        private Integer id;
        private Integer orderId;
        private Integer goodsId;
        private String goodsName;
        private String goodsSn;
        private Integer productId;
        private Short number;
        private BigDecimal price;
        private List<String> specifications;
        private String picUrl;
        private Integer comment;
        private Date addTime;
        private Date updateTime;
        private Boolean deleted;
    }

    public static OrderDetailData data(List<MarketOrderGoods> marketOrderGoods, MarketUser marketUser, MarketOrder marketOrder){
        OrderDetailData orderDetailData = new OrderDetailData(new ArrayList<>(),new UserDTO(),new OrderDTO());
        try {
            BeanUtils.copyProperties(orderDetailData.getUser(),marketUser);
            BeanUtils.copyProperties(orderDetailData.getOrder(),marketOrder);
//            BeanUtils.copyProperties(detailData.orderGoods,marketOrderGoods); //❌
            for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
                OrderGoodsDTO orderGoods = new OrderGoodsDTO();
                BeanUtils.copyProperties(orderGoods,marketOrderGood);
                orderDetailData.getOrderGoods().add(orderGoods);
            }
            return orderDetailData;
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
